import Vue from 'vue'
import Vuex from 'vuex'
// import {
// 	getUserInfo
// } from '@/api/user'
Vue.use(Vuex)

let lifeData = {}

try {
	// 尝试获取本地是否存在lifeData变量，第一次启动APP时是不存在的
	lifeData = uni.getStorageSync('lifeData')
} catch (e) {}
// 需要永久存储，且下次APP启动需要取出的，在state中的变量名
let saveStateKeys = ['winningStyle']

// 保存变量到本地存储中
const saveLifeData = function(key, value) {
	// 判断变量名是否在需要存储的数组中
	if (saveStateKeys.indexOf(key) != -1) {
		// 获取本地存储的lifeData对象，将变量添加到对象中
		let tmp = uni.getStorageSync('lifeData')
		// 第一次打开APP，不存在lifeData变量，故放一个{}空对象
		tmp = tmp ? tmp : {}
		tmp[key] = value
		// 执行这一步后，所有需要存储的变量，都挂载在本地的lifeData对象中
		uni.setStorageSync('lifeData', tmp)
	}
}

const store = new Vuex.Store({
	state: {
		winningStyle: 1, //开奖风格
		fixed: false,
		balls: [{
			opencode: '01',
			wave: 'red',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合单', '小单', '合小', '尾小'],
			line3: '红波',
			line4: '红单',
			line5: '龙',
			line6: '0头',
			line7: '1尾',
			line8: '火',
		}, {
			opencode: '02',
			wave: 'red',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合双', '小双', '合小', '尾小'],
			line3: '红波',
			line4: '红双',
			line5: '兔',
			line6: '0头',
			line7: '2尾',
			line8: '金',
		}, {
			opencode: '03',
			wave: 'blue',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合单', '小单', '合小', '尾小'],
			line3: '蓝波',
			line4: '蓝单',
			line5: '虎',
			line6: '0头',
			line7: '3尾',
			line8: '金',
		}, {
			opencode: '04',
			wave: 'blue',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合双', '小双', '合小', '尾小'],
			line3: '蓝波',
			line4: '蓝双',
			line5: '牛',
			line6: '0头',
			line7: '4尾',
			line8: '土',
		}, {
			opencode: '05',
			wave: 'green',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合单', '小单', '合小', '尾大'],
			line3: '绿波',
			line4: '绿单',
			line5: '鼠',
			line6: '0头',
			line7: '5尾',
			line8: '土',
		}, {
			opencode: '06',
			wave: 'green',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合双', '小双', '合小', '尾大'],
			line3: '绿波',
			line4: '绿双',
			line5: '猪',
			line6: '0头',
			line7: '6尾',
			line8: '木',
		}, {
			opencode: '07',
			wave: 'red',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合单', '小单', '合大', '尾大'],
			line3: '红波',
			line4: '红单',
			line5: '狗',
			line6: '0头',
			line7: '7尾',
			line8: '木',
		}, {
			opencode: '08',
			wave: 'red',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合双', '小双', '合大', '尾大'],
			line3: '红波',
			line4: '红双',
			line5: '鸡',
			line6: '0头',
			line7: '8尾',
			line8: '火',
		}, {
			opencode: '09',
			wave: 'blue',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合单', '小单', '合大', '尾大'],
			line3: '蓝波',
			line4: '蓝单',
			line5: '猴',
			line6: '0头',
			line7: '9尾',
			line8: '火',
		}, {
			opencode: '10',
			wave: 'blue',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合单', '小双', '合小', '尾小'],
			line3: '蓝波',
			line4: '蓝双',
			line5: '羊',
			line6: '1头',
			line7: '0尾',
			line8: '金',
		}, {
			opencode: '11',
			wave: 'green',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合双', '小单', '合小', '尾小'],
			line3: '绿波',
			line4: '绿单',
			line5: '马',
			line6: '1头',
			line7: '1尾',
			line8: '金',
		}, {
			opencode: '12',
			wave: 'red',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合单', '小双', '合小', '尾小'],
			line3: '红波',
			line4: '红双',
			line5: '蛇',
			line6: '1头',
			line7: '2尾',
			line8: '水',
		}, {
			opencode: '13',
			wave: 'red',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合双', '小单', '合小', '尾小'],
			line3: '红波',
			line4: '红单',
			line5: '龙',
			line6: '1头',
			line7: '3尾',
			line8: '水',
		}, {
			opencode: '14',
			wave: 'blue',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合单', '小双', '合小', '尾小'],
			line3: '蓝波',
			line4: '蓝双',
			line5: '兔',
			line6: '1头',
			line7: '4尾',
			line8: '木',
		}, {
			opencode: '15',
			wave: 'blue',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合双', '小单', '合小', '尾大'],
			line3: '蓝波',
			line4: '蓝单',
			line5: '虎',
			line6: '1头',
			line7: '5尾',
			line8: '木',
		}, {
			opencode: '16',
			wave: 'green',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合单', '小双', '合大', '尾大'],
			line3: '绿波',
			line4: '绿双',
			line5: '牛',
			line6: '1头',
			line7: '6尾',
			line8: '火',
		}, {
			opencode: '17',
			wave: 'green',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合双', '小单', '合大', '尾大'],
			line3: '绿波',
			line4: '绿单',
			line5: '鼠',
			line6: '1头',
			line7: '7尾',
			line8: '火',
		}, {
			opencode: '18',
			wave: 'red',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合单', '小双', '合大', '尾大'],
			line3: '红波',
			line4: '红双',
			line5: '猪',
			line6: '1头',
			line7: '8尾',
			line8: '土',
		}, {
			opencode: '19',
			wave: 'red',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合双', '小单', '合大', '尾大'],
			line3: '红波',
			line4: '红单',
			line5: '狗',
			line6: '1头',
			line7: '9尾',
			line8: '土',
		}, {
			opencode: '20',
			wave: 'blue',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合双', '小双', '合小', '尾小'],
			line3: '蓝波',
			line4: '蓝双',
			line5: '鸡',
			line6: '2头',
			line7: '0尾',
			line8: '水',
		}, {
			opencode: '21',
			wave: 'green',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合单', '小单', '合小', '尾小'],
			line3: '绿波',
			line4: '绿单',
			line5: '猴',
			line6: '2头',
			line7: '1尾',
			line8: '水',
		}, {
			opencode: '22',
			wave: 'green',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合双', '小双', '合小', '尾小'],
			line3: '绿波',
			line4: '绿双',
			line5: '羊',
			line6: '2头',
			line7: '2尾',
			line8: '木',
		}, {
			opencode: '23',
			wave: 'red',
			show: false,
			line1left: '单',
			line1right: '小',
			line2: ['合单', '小单', '合小', '尾小'],
			line3: '红波',
			line4: '红单',
			line5: '马',
			line6: '2头',
			line7: '3尾',
			line8: '木',
		}, {
			opencode: '24',
			wave: 'red',
			show: false,
			line1left: '双',
			line1right: '小',
			line2: ['合双', '小双', '合小', '尾小'],
			line3: '红波',
			line4: '红双',
			line5: '蛇',
			line6: '2头',
			line7: '4尾',
			line8: '金',
		}, {
			opencode: '25',
			wave: 'blue',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合单', '大单', '合大', '尾大'],
			line3: '蓝波',
			line4: '蓝单',
			line5: '龙',
			line6: '2头',
			line7: '5尾',
			line8: '金',
		}, {
			opencode: '26',
			wave: 'blue',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合双', '大双', '合大', '尾大'],
			line3: '蓝波',
			line4: '蓝双',
			line5: '兔',
			line6: '2头',
			line7: '6尾',
			line8: '土',
		}, {
			opencode: '27',
			wave: 'green',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合单', '大单', '合大', '尾大'],
			line3: '绿波',
			line4: '绿单',
			line5: '虎',
			line6: '2头',
			line7: '7尾',
			line8: '土',
		}, {
			opencode: '28',
			wave: 'green',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合双', '大双', '合大', '尾大'],
			line3: '绿波',
			line4: '绿双',
			line5: '牛',
			line6: '2头',
			line7: '8尾',
			line8: '水',
		}, {
			opencode: '29',
			wave: 'red',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合单', '大单', '合大', '尾大'],
			line3: '红波',
			line4: '红单',
			line5: '鼠',
			line6: '2头',
			line7: '9尾',
			line8: '水',
		}, {
			opencode: '30',
			wave: 'red',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合单', '大双', '合小', '尾小'],
			line3: '红波',
			line4: '红双',
			line5: '猪',
			line6: '3头',
			line7: '0尾',
			line8: '火',
		}, {
			opencode: '31',
			wave: 'blue',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合双', '大单', '合小', '尾小'],
			line3: '蓝波',
			line4: '蓝单',
			line5: '狗',
			line6: '3头',
			line7: '1尾',
			line8: '火',
		}, {
			opencode: '32',
			wave: 'green',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合单', '大双', '合小', '尾小'],
			line3: '绿波',
			line4: '绿双',
			line5: '鸡',
			line6: '3头',
			line7: '2尾',
			line8: '金',
		}, {
			opencode: '33',
			wave: 'green',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合双', '大单', '合小', '尾小'],
			line3: '绿波',
			line4: '绿单',
			line5: '猴',
			line6: '3头',
			line7: '3尾',
			line8: '金',
		}, {
			opencode: '34',
			wave: 'red',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合单', '大双', '合大', '尾小'],
			line3: '红波',
			line4: '红双',
			line5: '羊',
			line6: '3头',
			line7: '4尾',
			line8: '土',
		}, {
			opencode: '35',
			wave: 'red',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合双', '大单', '合大', '尾大'],
			line3: '红波',
			line4: '红单',
			line5: '马',
			line6: '3头',
			line7: '5尾',
			line8: '土',
		}, {
			opencode: '36',
			wave: 'blue',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合单', '大双', '合大', '尾大'],
			line3: '蓝波',
			line4: '蓝双',
			line5: '蛇',
			line6: '3头',
			line7: '6尾',
			line8: '木',
		}, {
			opencode: '37',
			wave: 'blue',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合双', '大单', '合大', '尾大'],
			line3: '蓝波',
			line4: '蓝单',
			line5: '龙',
			line6: '3头',
			line7: '7尾',
			line8: '木',
		}, {
			opencode: '38',
			wave: 'green',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合单', '大双', '合大', '尾大'],
			line3: '绿波',
			line4: '绿双',
			line5: '兔',
			line6: '3头',
			line7: '8尾',
			line8: '火',
		}, {
			opencode: '39',
			wave: 'green',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合双', '大单', '合大', '尾大'],
			line3: '绿波',
			line4: '绿单',
			line5: '虎',
			line6: '3头',
			line7: '9尾',
			line8: '火',
		}, {
			opencode: '40',
			wave: 'red',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合双', '大双', '合小', '尾小'],
			line3: '红波',
			line4: '红双',
			line5: '牛',
			line6: '4头',
			line7: '0尾',
			line8: '金',
		}, {
			opencode: '41',
			wave: 'blue',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合单', '大单', '合小', '尾小'],
			line3: '蓝波',
			line4: '蓝单',
			line5: '鼠',
			line6: '4头',
			line7: '1尾',
			line8: '金',
		}, {
			opencode: '42',
			wave: 'blue',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合双', '大双', '合小', '尾小'],
			line3: '蓝波',
			line4: '蓝双',
			line5: '猪',
			line6: '4头',
			line7: '2尾',
			line8: '水',
		}, {
			opencode: '43',
			wave: 'green',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合单', '大单', '合大', '尾小'],
			line3: '绿波',
			line4: '绿单',
			line5: '狗',
			line6: '4头',
			line7: '3尾',
			line8: '水',
		}, {
			opencode: '44',
			wave: 'green',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合双', '大双', '合大', '尾小'],
			line3: '绿波',
			line4: '绿双',
			line5: '鸡',
			line6: '4头',
			line7: '4尾',
			line8: '木',
		}, {
			opencode: '45',
			wave: 'red',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合单', '大单', '合大', '尾大'],
			line3: '红波',
			line4: '红单',
			line5: '猴',
			line6: '4头',
			line7: '5尾',
			line8: '木',
		}, {
			opencode: '46',
			wave: 'red',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合双', '大双', '合大', '尾大'],
			line3: '红波',
			line4: '红双',
			line5: '羊',
			line6: '4头',
			line7: '6尾',
			line8: '火',
		}, {
			opencode: '47',
			wave: 'blue',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合单', '大单', '合大', '尾大'],
			line3: '蓝波',
			line4: '蓝单',
			line5: '马',
			line6: '4头',
			line7: '7尾',
			line8: '火',
		}, {
			opencode: '48',
			wave: 'blue',
			show: false,
			line1left: '双',
			line1right: '大',
			line2: ['合双', '大双', '合大', '尾大'],
			line3: '蓝波',
			line4: '蓝双',
			line5: '蛇',
			line6: '4头',
			line7: '8尾',
			line8: '土',
		}, {
			opencode: '49',
			wave: 'green',
			show: false,
			line1left: '单',
			line1right: '大',
			line2: ['合单', '大单', '合大', '尾大'],
			line3: '绿波',
			line4: '绿单',
			line5: '龙',
			line6: '4头',
			line7: '9尾',
			line8: '土',
		}]
	},
	mutations: {
		$uStore(state, payload) {
			// 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
			let nameArr = payload.name.split('.')
			let saveKey = ''
			let len = nameArr.length
			if (len >= 2) {
				let obj = state[nameArr[0]]
				for (let i = 1; i < len - 1; i++) {
					obj = obj[nameArr[i]]
				}
				obj[nameArr[len - 1]] = payload.value
				saveKey = nameArr[0]
			} else {
				// 单层级变量，在state就是一个普通变量的情况
				state[payload.name] = payload.value
				saveKey = payload.name
			}
			// 保存变量到本地，见顶部函数定义
			saveLifeData(saveKey, state[saveKey])
		},
	},
	getters: {
		//根据数字获取球具体信息
		getBallInfo(state) {
			return (number) => {
				// console.log('numbernumber', typeof number);
				// console.log('JSON.parse(JSON.stringify(state.balls.find(item => item.opencode == number)))',state,number);

				// 正则表达式
				const chineseRegex = /^[\u4e00-\u9fa5]+$/; // 匹配全中文
				const numberRegex = /^[0-9]+$/; // 匹配全数字
				if (chineseRegex.test(number)) {
					return "中文";
				} else if (numberRegex.test(number)) {
					if (number) {
						let ball = JSON.parse(JSON.stringify(state.balls.find(item => item.opencode ==
							number)))
						ball.zodiac = ball.line5
						return ball
					}
				} else {
					return "其他";
				}



			}
		},
		judgingStyle: (state) => (val) => {
			const baseStyle = 'color: rgb(0, 0, 0); font-size: 0.50rem; font-weight: bold;';
			
			// Style 1 和 2 的映射配置
			const styleMap = {
				1: {
					red: `background: url(../../static/a_h.png) 0% center / 100% 100%; ${baseStyle}`,
					blue: `background: url(../../static/a_l.png) 0% center / 100% 100%; ${baseStyle}`,
					green: `background: url(../../static/a_n.png) 0% center / 100% 100%; ${baseStyle}`
				},
				2: {
					red: `background: url(../../static/b_r.png) 0% center / 100% 100%; ${baseStyle}`,
					blue: `background: url(../../static/b_l.png) 0% center / 100% 100%; ${baseStyle}`,
					green: `background: url(../../static/b_n.png) 0% center / 100% 100%; ${baseStyle}`
				}
			};

			// Style 3 的特殊处理
			if (state.winningStyle === 3) {
				return `background: ${val}; color: rgb(255, 255, 255); font-size: 0.96rem; width: 1.2rem; height: 1.28rem`;
			}

			// 返回对应的样式
			return styleMap[state.winningStyle]?.[val] || '';
		}
	},
	actions: {
		// //获取用户个人信息
		// async getUserInfo({ commit, state }) {
		//   return await new Promise((resolve, reject) => {
		//     getUserInfo({
		//       uid: state.uid,
		//     })
		//       .then((res) => {
		//         const userInfo = res.data
		//         uni.$u.vuex('userInfo', userInfo)
		//         resolve(userInfo)
		//       })
		//       .catch((err) => {
		//         reject(err)
		//       })
		//   })
		// },
		// 改变开奖类型
		changeWinningStyle({
			commit,
			state
		}) {
			uni.$u.vuex('winningStyle', state.winningStyle != 3 ? state.winningStyle += 1 : 1)
		}
	},
})

export default store